<?php
/**
 * Copyright (c) 2006-2008, Julien PORTALIER
 * http://featherphp.googlecode.com/
 * 
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 */

class translate
{
	static private $lang     = Core::locale_default;
	static private $ctx;
	static private $messages = array();
	static private $plurals  = array(
		'en' => '($n != 1) ? 1 : 0',
		'fr' => '($n >  1) ? 1 : 0',
	);
	
	/**
	 * Ignits the translation system.
	 * Preloads the global context.
	 * 
	 * @param $lang String[optional]
	 */
	static function init($lang, $ctx)
	{
		translate::$lang = $lang;
		translate::$ctx  = $ctx;
		
		translate::load('global');
		translate::load($ctx);
	}
	
	/**
	 * Loads translation strings for the given context.
	 * 
	 * @param $ctx String[optional]
	 */
	static function load($ctx)
	{
#		$file = APP.'locales'.DS.translate::$lang.DS.'json'.DS."$ctx.json";
		list($_ctx, ) = explode('.', $ctx, 2);
		$file = APP.'locales'.DS.translate::$lang.DS."{$_ctx}.json";
		
		if (file_exists($file))
		{
			$json    = file_get_contents($file);
#			translate::$messages[$ctx] = json_decode($json, true);
			$strings = json_decode($json, true);
			translate::$messages = array_merge(translate::$messages, $strings);
		}
		else {
			translate::$messages[$ctx] = array();
		}
	}
	
	/**
	 * Translates a phrase.
	 * 
	 * @return String The translated phrase.
	 * @param $msgid String The phrase to translate.
	 * @param $ctx String[optional] Defaults to the global context.
	 */
	static function singular($msgid, $ctx=null)
	{
		# loads translation strings
		if (!isset(translate::$messages[$ctx])) {
			translate::load($ctx);
		}
		
		/*
		# 1. tries to translate the string in current context
		if (!empty(translate::$ctx)
			and isset(translate::$messages[translate::$ctx][$msgid]))
		{
			return translate::$messages[translate::$ctx][$msgid];
		}
		*/
		# 2. tries to translate the string in the given context
		if (!empty($ctx)
			and isset(translate::$messages[$ctx][$msgid]))
		{
			return translate::$messages[$ctx][$msgid];
		}
		
		# 3. tries to translate the string in the global context
		if (isset(translate::$messages['global'][$msgid])) {
			return translate::$messages['global'][$msgid];
		}
		
		# 4. no translation found
		return $msgid;
	}
	
	/**
	 * Translates a phrase, checking plural cases.
	 * 
	 * @return String The translated phrase.
	 * @param $msgid String The phrase to translate.
	 * @param $msgid_plural String The plural version of the phrase to translate.
	 * @param $n Integer
	 * @param $ctx String[optional] Defaults to the global context.
	 */
	static function plural($msgid, $msgid_plural, $n, $ctx=null)
	{
		# loads translation strings
		if (!isset(translate::$messages[$ctx])) {
			translate::load($ctx);
		}
		
		# computes which index to use
		eval('$index = '.translate::$plurals[translate::$lang].';');
		
		/*
		# 1. tries to translate the string in current context
		if (!empty(translate::$ctx)
			and isset(translate::$messages[translate::$ctx][$msgid][$index]))
		{
			return translate::$messages[translate::$ctx][$msgid][$index];
		}
		*/
		# 2. tries to translate the string in the given context
		if (!empty($ctx)
			and isset(translate::$messages[$ctx][$msgid][$index]))
		{
			return translate::$messages[$ctx][$msgid][$index];
		}
		
		# 3. tries to translate the string in the global context
		if (isset(translate::$messages['global'][$msgid][$index])) {
			return translate::$messages['global'][$msgid][$index];
		}
		
		# 4. no translation found
		return ($index == 0) ? $msgid : $msgid_plural;
	}
}

/**
 * @see translate::singular()
 */
function t($msgid, $ctx=null)
{
	return translate::singular($msgid, $ctx);
}

/**
 * @see translate::plural()
 */
function tn($msgid, $msgid_plural, $n, $ctx=null)
{
	return translate::plural($msgid, $msgid_plural, $n, $ctx);
}

?>